package rai.portlet;

import java.io.IOException;
import java.util.List;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;

import rai.dao.*;
import rai.dto.*;

/**
 * Servlet implementation class SearchInfoServlet.
 * @author Vu
 */
public class SearchInfoServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;
    /** log property. */
    private static Logger log = Logger.getLogger("SearchInfoPortlet");

    /**
     * Default constructor.
     */
    public SearchInfoServlet() {
        // TODO Auto-generated constructor stub
    }

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
     *      response)
     */
    @Override
    protected final void doGet(final HttpServletRequest request,
            final HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        response.setContentType("text/xml");
        String Compid = request.getParameter("lstComp");
        String Name = request.getParameter("name");
        SearchInfoDAO dao = new SearchInfoDAO();
        ServletOutputStream a = response.getOutputStream();
        log.info(Name + " " + Compid);
        if (Compid != null && Name != null) {
            // request.getSession().setAttribute("lstCompID",Compid);

            log.info("Start DepartmentListServlet" + Compid);

            List<DepartmentDTO> list_dep = dao.getDep1(Compid);
            request.getSession().setAttribute("DepartmentList", list_dep);
            String id;
            try {
                id = (String) request.getSession(true).getAttribute("lstDept");
                log.info("value id " + Name + id);
            } catch (Exception e) {
                id = "0";
            }
            if (id == null) {
                id = "0";
            }
            log.info("value id " + id);
            if (Compid.equals("0")) {
                a.print("<select name=\"" + Name
                        + "\" disabled onchange=\"updateCourse(event)\">");
            } else {
                a.print("<select name=\"" + Name
                        + "\" onchange=\"updateCourse(event)\">");

            }
            for (int i = 0; i < list_dep.size(); i++) {
                DepartmentDTO exp = new DepartmentDTO();
                exp = list_dep.get(i);
                a.println("<option value=" + exp.getId() + ">" + exp.getSname()
                        + "</option>");

            }
            a.println("</select>");
            a.close();
            log.info("Stop DepartmentListServlet");
        }
        // Start updateCourse List
        String Departid = request.getParameter("lstDept");

        if (Departid != null) {
            log.info("Start CourseListServlet");
            List<CourseDTO> list_course = dao.getCourse(Departid);
            request.getSession().setAttribute("CourseList", list_course);
            if (Departid.equals("0")) {
                a.print("<select name=\"lstCourse\" disabled> ");
            } else {
                a.print("<select name=\"lstCourse\"> ");

            }
            for (int i = 0; i < list_course.size(); i++) {
                CourseDTO exp = new CourseDTO();
                exp = list_course.get(i);
                a.println("<option value=" + exp.getId() + ">" + exp.getSname()
                        + "</option>");
            }
            a.println("</select>");
            a.close();
            log.info("Stop CourseListServlet");
        }

        String DepartPrjid = request.getParameter("lstPrjDept");
        if (DepartPrjid != null) {
            log.info("Start ProjectListServlet");
            List<ProjectDTO> list_prj = dao.getPrj(DepartPrjid);
            request.getSession().setAttribute("ProjectList", list_prj);
            if (DepartPrjid.equals("0")) {
                a.print("<select name=\"lstPrj\" disabled> ");
            } else {
                a.print("<select name=\"lstPrj\"> ");

            }
            for (int i = 0; i < list_prj.size(); i++) {
                ProjectDTO exp = new ProjectDTO();
                exp = list_prj.get(i);
                a.println("<option value=" + exp.getId() + ">" + exp.getSname()
                        + "</option>");
            }
            a.println("</select>");
            a.close();
            log.info("Stop ProjectListServlet");
        }
        
    }

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
     *      response)
     */

    @Override
    protected final void doPost(final HttpServletRequest request,
            final HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        response.setContentType("text/xml");
        String Compid = request.getParameter("lstCompID");
        request.getSession().setAttribute("lstComp", Compid);
        RequestDispatcher dispatcher = request
                .getRequestDispatcher("/WEB-INF/jsp/SearchInfoPortlet_view.jsp");
        dispatcher.forward(request, response);
    }
}
